<?php
/**
 * Created by PhpStorm.
 * User: zws
 * Date: 2018/9/8
 * Time: 11:02
 * 检测仪器管理
 */
namespace app\index\service;

use app\common\model\InstrumentModel;
use think\Exception;

class InstrumentService
{
    protected static $instrumentModel;
    /**
     * UserService constructor.
     * @param InstrumentModel $instrumentModel
     * @author zws
     */
    public function __construct(InstrumentModel $instrumentModel)
    {
        self::$instrumentModel = $instrumentModel;
    }


    /**
     * 查询项目
     * @param $param
     * @return array
     * @author zws
     */
    public function index($param=array())
    {
        $result = self::$instrumentModel->where(['is_del'=>1, 'status'=>1])
            ->select();
        if($result){
            $instrument_type = config("param_config.instrument_type");
            $instrument_status = config("param_config.instrument_status");
            foreach ($result as $k=>$v)
            {
                $result[$k]['instrument_type'] = isset($instrument_type[$v->type]) ?
                    $instrument_type[$v->type] : '';
                $result[$k]['instrument_status'] = isset($instrument_status[$v->status]) ?
                    $instrument_status[$v->status] : '';
            }
        }
        return $result;
    }

    /**
     * 员工创建仪器
     * User: zhouyao
     * Date: 2018/9/30
     * Time: 下午4:25
     * @param $params
     * @return bool|void
     * @throws \app\common\exception\AppException
     */
    public function create($params)
    {
        if (empty($params)) {
            return app_fail(9997);
        }

        if (empty($params['instrument_num']) || empty($params['type']) || empty($params['version_num']) || empty($params['status'])) {
            return app_fail(9997);
        }

        $where = !empty($params['id']) ? ['id'=>$params['id']] : [];
        try{
            $result = self::$instrumentModel->allowField(true)->save($params, $where);
            return $result;
        } catch (Exception $e) {
            app_fail(9899);//仪器编辑失败
        }
    }


}